package com.gable.report.service.impl;

import com.gable.report.common.util.MonthEnumUtil;
import com.gable.report.dao.IChartsForHospitalMaintainerDao;
import com.gable.report.model.ChartsAssetsMaintainInfoView;
import com.gable.report.model.ChartsForMaintainerView;
import com.gable.report.model.ChartsHospitalQuery;
import com.gable.report.service.ChartsForHospitalMaintainerService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import static com.gable.report.util.ChartsUtil.getParamMap;


/**
 * Created by lvdouStart on 2017/7/26.
 * 医院报表维修商service实现类
 */
@Service
public class ChartsForHospitalMaintainerServiceImpl implements ChartsForHospitalMaintainerService {

    private final Log logger = LogFactory.getLog(this.getClass());

    @Autowired
    IChartsForHospitalMaintainerDao chartsForHospitalMaintainerDao;


    @Override
    public Map<String, Object> queryAllAssetsAndRepairSizes(Long hospitalId, ChartsHospitalQuery chartsHospitalQuery) throws Exception {
        Map<String,Object> returnMap = new HashMap<>();
        Map<String,Object> paramMap = getParamMap(hospitalId, chartsHospitalQuery);
        ChartsForMaintainerView allAssets = chartsForHospitalMaintainerDao.queryAllAssets(paramMap);
        List<ChartsForMaintainerView> list = chartsForHospitalMaintainerDao.queryRepairSizes(paramMap);
        List<ChartsForMaintainerView> maintainSizeList = new ArrayList<>();
        //查询结果可能发生缺少的情况 通过下面方法补全
        List<String> maintainSizeNameList = Arrays.asList("大维修","中维修","小维修");
        for(String str : maintainSizeNameList){
            ChartsForMaintainerView cfmv = null;
            for(ChartsForMaintainerView obj : list){
                if(obj != null && obj.getName() != null && str.equals(obj.getName())){
                    cfmv = obj;
                }
            }
            if(cfmv == null){
                cfmv = new ChartsForMaintainerView();
                cfmv.setName(str);
            }
            maintainSizeList.add(cfmv);
        }
        returnMap.put("allAssets",allAssets);
        returnMap.put("list",maintainSizeList);
        return returnMap;
    }

    @Override
    public Map<String, Object> queryDepartments(Long hospitalId, ChartsHospitalQuery chartsHospitalQuery) throws Exception {
        Map<String,Object> returnMap = new HashMap<>();
        Map<String,Object> paramMap = getParamMap(hospitalId, chartsHospitalQuery);
        ChartsForMaintainerView allAssets = chartsForHospitalMaintainerDao.queryAllAssets(paramMap);
        List<ChartsForMaintainerView> list = chartsForHospitalMaintainerDao.queryDepartments(paramMap);
        double avgToRepairCounts = 0.0;
        if(allAssets != null && list != null && list.size() != 0){
            avgToRepairCounts = allAssets.getCurTotalRepairCounts()/list.size()*1.0;
        }
        returnMap.put("avgToRepairCounts",avgToRepairCounts);
        returnMap.put("list",list);
        return returnMap;
    }

    @Override
    public Map<String, Object> queryDeviceModels(Long hospitalId, ChartsHospitalQuery chartsHospitalQuery) throws Exception {
        Map<String,Object> returnMap = new HashMap<>();
        Map<String,Object> paramMap = getParamMap(hospitalId, chartsHospitalQuery);
        List<ChartsForMaintainerView> list = chartsForHospitalMaintainerDao.queryDeviceModels(paramMap);
        returnMap.put("list",list);
        return returnMap;
    }

    @Override
    public Map<String, Object> queryDeviceTypes(Long hospitalId, ChartsHospitalQuery chartsHospitalQuery) throws Exception {
        Map<String,Object> returnMap = new HashMap<>();
        Map<String,Object> paramMap = getParamMap(hospitalId, chartsHospitalQuery);
        List<ChartsForMaintainerView> list = chartsForHospitalMaintainerDao.querySecondDeviceTypes(paramMap);
        ChartsForMaintainerView allAssets = chartsForHospitalMaintainerDao.queryAllAssets(paramMap);
        double avgToRepairCounts = 0.0;
        if(allAssets != null && list != null && list.size() != 0){
            avgToRepairCounts = allAssets.getCurTotalRepairCounts()/list.size()*1.0;
        }
        returnMap.put("avgToRepairCounts",avgToRepairCounts);
        returnMap.put("list",list);
        return returnMap;
    }

    @Override
    public Map<String, Object> queryDeviceBrands(Long hospitalId, ChartsHospitalQuery chartsHospitalQuery) throws Exception {
        return null;
    }

    @Override
    public Map<String, Object> queryForMonth(Long hospitalId, ChartsHospitalQuery chartsHospitalQuery) throws Exception {
        Map<String,Object> returnMap = new HashMap<>();
        Map<String,Object> paramMap = getParamMap(hospitalId, chartsHospitalQuery);
        List<ChartsForMaintainerView> list = chartsForHospitalMaintainerDao.queryForMonth(paramMap);
        for(ChartsForMaintainerView obj : list){
            obj.setName(MonthEnumUtil.getValue(obj.getId()));
        }
        returnMap.put("list",list);
        return returnMap;
    }

    @Override
    public Map<String, Object> queryMaintainers(Long hospitalId, ChartsHospitalQuery chartsHospitalQuery) throws Exception {
        Map<String,Object> returnMap = new HashMap<>();
        Map<String,Object> paramMap = getParamMap(hospitalId, chartsHospitalQuery);
        List<ChartsForMaintainerView> list = chartsForHospitalMaintainerDao.queryMaintainers(paramMap);
        returnMap.put("list",list);
        return returnMap;
    }

    @Override
    public Map<String, Object> queryDeviceInfoByModelName(Long hospitalId, ChartsHospitalQuery chartsHospitalQuery) throws Exception {
        Map<String,Object> returnMap = new HashMap<>();
        Map<String,Object> paramMap = getParamMap(hospitalId, chartsHospitalQuery);
        List<ChartsAssetsMaintainInfoView> list = chartsForHospitalMaintainerDao.queryDeviceInfoByModelName(paramMap);
        returnMap.put("list",list);
        return returnMap;
    }

}
